<?php
declare (strict_types = 1);

namespace app\controller;

use think\Request;
use think\facade\Db;
class Cart
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
		$res = Db::name("cart")->join("user","cart.userId=user.id")->join("sku","sku.id=cart.skuId")->join("goods","goods.id=sku.goodsId")->field("cart.id,cart.goodsNum,cart.createTime,cart.updateTime,cart.checked,sku.name as skuName,sku.id as skuId,goods.name as gName,goods.id as goodsId,user.name as uName,user.id as userId,sku.price")->select();
		return json(["code"=>200,"data"=>$res]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
		$p = input("post."); 
		$date = date('Y-m-d H:i:s',time());
		$d = ["skuId"=>$p["skuId"],"goodsNum"=>$p["goodsNum"],"checked"=>$p["checked"],"userId"=>$p["userId"],"createTime"=>$date,"updateTime"=>$date];
		Db::table("cart")->insert($d);
		return json(["code"=>200,"msg"=>"操作成功"]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update()
    {
        //
		$p = input("post.");
		$date = date('Y-m-d H:i:s',time());
		$a = ["updateTime"=>$date];
		$d = array_merge($p,$a);
		Db::table("cart")->save($d);
		return json(["code"=>200,"msg"=>"操作成功"]);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete()
    {
        //
		$p = input("get.");
		Db::table("cart")->where("id",$p["id"])->delete();
		return json(["code"=>200,"msg"=>"操作成功"]);
    }
}
